package com.haixee.parkdemo.data_acl.process;

import com.haixee.parkdemo.common.context.WebContextHolder;
import com.haixee.parkdemo.common.feign.auth.AuthRPCService;
import com.haixee.parkdemo.common.response.ConPostProcessor;
import com.haixee.parkdemo.common.response.ResResult;
import com.haixee.parkdemo.data_acl.annotation.DataAclSetting;
import lombok.SneakyThrows;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class DataAccessProcessor implements ConPostProcessor {

    @Autowired(required = false)
    private AuthRPCService authRPCService;

    @SneakyThrows
    @Override
    public void process(ResResult result, MethodSignature methodSignature) {

        if (authRPCService == null) {
            return;
        }

        if (!(result.getData() instanceof List)) {
            return;
        }

        List<String> fieldList = WebContextHolder.getAuth().getFieldList();
        if (fieldList.size() == 0) {
            return;
        }



//        Integer aclId = WebContextHolder.getAclId();

//        Map<String, String> configurableNameMap = new HashMap<>();
        Method method = methodSignature.getMethod();
        DataAclSetting classNameAnnotation = method.getAnnotation(DataAclSetting.class);
        if (classNameAnnotation == null) {
            return;
        }

        Class<?> classData = classNameAnnotation.voClass();

        Map<String, Field> fieldMap = new HashMap<>();

        for (String fieldName : fieldList) {
            Field declaredField = classData.getDeclaredField(fieldName);
            declaredField.setAccessible(true);
            fieldMap.put(fieldName, declaredField);
        }

        List<?> data = (List<?>) result.getData();
        for (Object dataItem : data) {
            for (Map.Entry<String, Field> fieldEntry : fieldMap.entrySet()) {
                Field field = fieldEntry.getValue();
                String type = field.getGenericType().getTypeName();
                if ("java.lang.String".equals(type)) {
                    fieldEntry.getValue().set(dataItem, "");
                }
            }
        }


//        for (Field field : classData.getDeclaredFields()) {
//            Configurable annotation = field.getAnnotation(Configurable.class);
//            if (annotation != null) {
//                configurableNameMap.put(field.getName(), annotation.value());
//            }
//        }
//
//        if (configurableNameMap.size() == 0) {
//            return;
//        }

//        Map<String, List<FieldRule>> fieldRules = authRPCService.getFieldRules(aclId, configurableNameMap.keySet());
//        dealData((List<?>) result.getData(), fieldRules, classData);
//        addConfigureData(result, configurableNameMap, fieldRules);
    }


//    private void dealData(List<?> listData, Map<String, List<FieldRule>> fieldRules, Class<?> classData) throws IllegalAccessException, NoSuchFieldException {
//
//        if (listData.size() == 0) {
//            return;
//        }
//
//        if (fieldRules.size() == 0) {
//            return;
//        }
//
//        Field optUidField = classData.getDeclaredField("optUid");
//        Field optOrgField = classData.getDeclaredField("optJob");
//        optUidField.setAccessible(true);
//        optOrgField.setAccessible(true);
//
//        Map<String, Field> fieldMap = new HashMap<>();
//
//        for (String fieldName : fieldRules.keySet()) {
//            Field field = classData.getDeclaredField(fieldName);
//            field.setAccessible(true);
//            fieldMap.put(fieldName, field);
//        }
//
//        Integer curUid = WebContextHolder.getUid();
//        Integer jobId = WebContextHolder.getJobId();
//        List<Integer> underJobList = WebContextHolder.getUnderJobList();
//
//        // 开始匹配 修改数据
//        for (Object objectData : listData) {
//
//            Integer optUid = (Integer) optUidField.get(objectData);
//            Integer optOrg = (Integer) optOrgField.get(objectData);
//
//            for (Map.Entry<String, List<FieldRule>> item : fieldRules.entrySet()) {
//
//                String name = item.getKey();
//                List<FieldRule> rules = item.getValue();
//
//                Field field = fieldMap.get(name);
//                Object o = field.get(objectData);
//
//                for (FieldRule rule : rules) {
//                    // 是否应用
//                    boolean apply = false;
//                    if (rule.getSelfApply() && Objects.equals(optUid, curUid)) {
//                        apply = true;
//                    }
//                    if (rule.getSameLevelApply() && !Objects.equals(optUid, curUid) && Objects.equals(jobId, optOrg)) {
//                        apply = true;
//                    }
//                    if (rule.getUpLevelApply() && !Objects.equals(jobId, optOrg) && underJobList.contains(optOrg)) {
//                        apply = true;
//                    }
//
//                    if (!apply) {
//                        continue;
//                    }
//
//                    // todo
//                    if (rule.getMatchType() == 1) {
//                        String raw = (String) o;
//                        o = raw.replaceAll(rule.getExpress(), rule.getExpressResult());
//                    }
//
//                }
//
//                field.set(objectData, o);
//            }
//        }
//    }


//    private void addConfigureData(ResResult result, Map<String, String> configurableNameMap, Map<String, List<FieldRule>> fieldRules) throws NoSuchFieldException, IllegalAccessException {
//
//        List<VoName> voNameList = new ArrayList<>();
//        for (Map.Entry<String, String> item : configurableNameMap.entrySet()) {
//            VoName voName = new VoName(item.getKey(), item.getValue());
//            voNameList.add(voName);
//        }
//
//        Field additionalField = result.getClass().getDeclaredField("additionalData");
//        additionalField.setAccessible(true);
//
//        Map<String, Object> mapData = (Map<String, Object>) additionalField.get(result);
//
//        if (mapData == null) {
//            mapData = new HashMap<>();
//        }
//
//        List<FieldRule> fieldRuleList = new ArrayList<>();
//        for (List<FieldRule> value : fieldRules.values()) {
//            fieldRuleList.addAll(value);
//        }
//
//        mapData.put("voName", voNameList);
//        mapData.put("fieldSetting", fieldRuleList);
//        additionalField.set(result, mapData);
//    }
}
